
package com.liuceng.zen.pojo.form;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.CaseFormat;

/**
 * 搜索表单公共属性
 */
public class BaseTableForm {

  /**
   * 开始索引
   */
  protected int iDisplayStart;
  /**
   * 要显示的数量
   */
  protected int iDisplayLength;
  /**
   * 所有可以排序的列
   */
  protected List<String> sColumns;
  /**
   * 要排序列数
   */
  protected Integer iSortingCols;
  /**
   * 排序信息,使用set方法注入
   */
  protected String sortInfo;
  /**
   * 全局搜索
   */
  protected String sSearch;
  /**
   * 排序的列名
   */
  protected String mDataProp;
  /**
   * 开始时间
   */
  protected String sSearch_0;//手机号 2016-07-01
  /**
   * 结束时间
   */
  protected String sSearch_1;

  /**
   * 创建时间(天)
   */
  protected String sSearch_2;


  /**
   * 表单统计快捷查询状态
   */
  protected String sSearch_3;

  protected String sSearch_4;

  /**
   * 新增姓名和手机号模糊查询 by zhengxianlei 2016-07-01
   *
   */
  protected String sSearch_5;//姓名


  public String  getsSearch_0() {
    return sSearch_0;
  }

  public void setsSearch_0(String sSearch_0) {
    this.sSearch_0 = sSearch_0;
  }

  public String getsSearch_1() {
    return sSearch_1;
  }

  public void setsSearch_1(String sSearch_1) {
    this.sSearch_1 = sSearch_1;
  }

  public String getsSearch_3() {
    return sSearch_3;
  }

  public void setsSearch_3(String sSearch_3) {
    this.sSearch_3 = sSearch_3;
  }

  public String getsSearch_4() {
    return sSearch_4;
  }

  public void setsSearch_4(String sSearch_4) {
    this.sSearch_4 = sSearch_4;
  }

  //新增查询手机号和姓名
  public String getsSearch_5() {
    return sSearch_5;
  }

  public void setsSearch_5(String sSearch_5) {
    this.sSearch_5 = sSearch_5;
  }


  public Date getDate(String date) {
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    if (date == null || date.equals(""))
      return null;
    else {
      try {
        return simple.parse(date);
      } catch (ParseException e) {
        return null;
      }
    }
  }


  /**
   * 排序信息，排序列格式转换，Controller中设置值：form.setSortInfo(request);
   * 
   * <pre>
   * 例: sSortDir_0 = 1 sSortDir_1 = 2 iSortCol_0 = desc iSortCol_1 = asc columns =
   * [id,userName,no,time,] 格式化返回: result = "userName_desc,no_asc" OR "userName desc,no asc" 依赖属性:
   * sSortDir_0..n 要排序列编号，参考字段: {@link #sColumns} iSortCol_0..n 要排序列排序方式: desc OR asc iSortingCols
   * 要排序列数量
   * 
   * <pre>
   * 
   * @param request HttpServletRequest
   *
   */
  public void setSortInfo(HttpServletRequest request) {
    if (iSortingCols == null || iSortingCols == 0) {
      setSortInfo("");
      return;
    }
    StringBuilder result = new StringBuilder("");
    for (int i = 0; i < iSortingCols; i++) {
      int index = Integer.parseInt(request.getParameter("iSortCol_" + i));
      String sortType = request.getParameter("sSortDir_" + i);
      // 正常条件排序
      setmDataProp(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,
          request.getParameter("mDataProp_" + index)));
      result.append(sColumns.get(index));
      result.append(" ");
      if (StringUtils.equalsIgnoreCase(sortType, "desc")
          || StringUtils.equalsIgnoreCase(sortType, "asc")) {
        result.append(sortType);
      }
      result.append(",");
    }

    setSortInfo(StringUtils.removeEnd(result.toString(), ",")); // 删除结尾逗号
  }

  /**
   * 默认不使用
   * 
   * @param sortInfo
   */
  public void setSortInfo(String sortInfo) {
    this.sortInfo = sortInfo;
  }

  /**
   * 获取排序信息 {@link #setSortInfo(javax.servlet.http.HttpServletRequest)}
   * 
   * 
   * @return 列名 + 空格 + 排序方式 + 逗号, 例: "userName_desc,no_asc" OR "userName desc,no asc"
   */
  public String getmDataProp() {
    return mDataProp;
  }

  public void setmDataProp(String mDataProp) {
    this.mDataProp = mDataProp;
  }

  public String getSortInfo() {
    return sortInfo;
  }

  public int getiDisplayStart() {
    return iDisplayStart;
  }

  public void setiDisplayStart(int iDisplayStart) {
    this.iDisplayStart = iDisplayStart;
  }

  public int getiDisplayLength() {
    return iDisplayLength;
  }

  public void setiDisplayLength(int iDisplayLength) {
    this.iDisplayLength = iDisplayLength;
  }

  public void setsColumns(List<String> sColumns) {
    this.sColumns = sColumns;
  }

  public Integer getiSortingCols() {
    return iSortingCols;
  }

  public void setiSortingCols(Integer iSortingCols) {
    this.iSortingCols = iSortingCols;
  }

  public String getsSearch() {
    return sSearch;
  }


  public String getsSearch_2() {
    return sSearch_2;
  }

  public void setsSearch_2(String sSearch_2) {
    this.sSearch_2 = sSearch_2;
  }

  public void setsSearch(String sSearch) {
    this.sSearch = sSearch;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("BaseTableForm [iDisplayStart=").append(iDisplayStart)
        .append(", iDisplayLength=").append(iDisplayLength).append(", sColumns=").append(sColumns)
        .append(", iSortingCols=").append(iSortingCols).append(",mDataProp=").append(mDataProp)
        .append(", sortInfo=").append(sortInfo).append("]");
    return builder.toString();
  }

  public String getName(String name) {
    StringBuilder sb = new StringBuilder(name);

    for (int i = 0; i < name.length(); i++) {
      if (name.charAt(i) >= 'A' && name.charAt(i) <= 'Z') {
        sb.replace(name.indexOf(name.charAt(i)), name.indexOf(name.charAt(i)) + 1,
            "_" + (char) (name.charAt(i) - 32));
      }

    }
    return sb.toString();
  }
}

